package cn.frh.pet.controller;

import cn.frh.basic.exception.BusinessException;
import cn.frh.basic.util.AjaxResult;
import cn.frh.basic.util.LoginContext;
import cn.frh.basic.util.PageList;
import cn.frh.org.domain.Department;
import cn.frh.org.domain.Shop;
import cn.frh.org.query.ShopQuery;
import cn.frh.pet.domain.Pet;
import cn.frh.pet.query.PetQuery;
import cn.frh.pet.service.IPetService;
import cn.frh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        return petService.queryPage(query);
    }


    //onsale/offsale
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            //获取当前登录人
            petService.offSale(ids);
            //service需要返回logininfo和token
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }

    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
        public Pet findById(@PathVariable("id") Long id){
        return petService.loadByIdDetail(id);
    }

    /**
     * 领养宠物
     * @param id 哪个宠物
     * @return
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id,Logininfo logininfo){
        try {
            petService.adopt(id,logininfo);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("骚瑞~操作失败！");
        }
    }


}
